﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using CourseUtils;

// http://www.platesh.ru/annuitetnie-plateshi/    business analysis

/* This is a new implementation of the credit calculator. 
1. Generating an instance of preparation inputs data and the date. 
2. Generation of a class instance prints the table header. 
3. Generation of class performs the calculation of all the fields of the table line. 
4. Storage of all the fields of the table is performed in a generic collection (LIST). 
5. Printing lines of the table is performed by printing each item of the collection. */


namespace creditCalculator
{
    public class CurrentPayment
    {
        public int creditSum;  // The sum which customer got from a creditor bank.
        public int rate;                          // This is the annual percentage rate.
        public int timePeriodOfCreditInMonth;
        public DateTime startDate;                // Date when a customer got a credit.
        public DateTime paymentDate;              // Date when a customer should make a current payment.
        public int numberOfPayment;               // The payment sequence number.
        public int numberOfString;
        public double bodyPart;                    // The body part of a current credit payment
        public double percentagesPart;               // The percentages part of a current credit payment
        public double debtAfterCurrentPayment;       // The remaining part of the current debt after payment by customer.
        public double AnnuityMonthlyPayment;         // A payment monthly sum which consist of 2 parts as creditPart and percentagesPart(?).

        public CurrentPayment(int numberOfPayment, int creditSum, int rate, int timePeriodOfCreditInMonth, DateTime startDate)
        {
            this.numberOfPayment = numberOfPayment;
            this.numberOfString = numberOfPayment;
            this.creditSum = creditSum;
            this.rate = rate;
            this.timePeriodOfCreditInMonth = timePeriodOfCreditInMonth;
            this.startDate = startDate;

            calculateAnnuityMonthlyPayment();
            for (int i = 1; i <= numberOfString; i++)
            {
                if (i == 1)
                {
                    this.debtAfterCurrentPayment = creditSum;
                }
                calculateNumberOfPayment();
                calculatePercentagesPart();
                calculateBodyPart();
                calculateDebtAfterCurrentPayment();
                calculatePaymentDate(numberOfPayment);
            }
        }

        public override string ToString()
        {
            return String.Format("| {0}\t|{1:ddd MMM dd, yyyy} | {2:f2}$\t| {3:f2}$\t | {4,14:f2}$ |", numberOfString,
                   paymentDate, bodyPart, percentagesPart, debtAfterCurrentPayment);
        }

        public CurrentPayment() { } // default constractor

        public void calculateNumberOfPayment()
        {
            numberOfPayment += 1;
            return;
        }

        public void calculatePaymentDate(int numberOfPayment)
        {
            paymentDate = startDate;
            paymentDate = paymentDate.AddMonths(numberOfPayment);
            if (paymentDate.DayOfWeek == DayOfWeek.Saturday)
            {
                paymentDate = paymentDate.AddDays(-1);
            }
            if (paymentDate.DayOfWeek == DayOfWeek.Sunday)
            {
                paymentDate = paymentDate.AddDays(-2);
            }
            return;
        }

        public void calculateAnnuityMonthlyPayment()
        { // calculation according to the main big formular.
            double monthlyRate = (double)(rate * 0.01 / 12.0);
            double intermediatePart = (double)Math.Pow(1.0 + monthlyRate, timePeriodOfCreditInMonth);
            AnnuityMonthlyPayment = creditSum * (monthlyRate + monthlyRate / (intermediatePart - 1));
            return;
        }

        public void calculateDebtAfterCurrentPayment()
        {
            debtAfterCurrentPayment -= AnnuityMonthlyPayment - percentagesPart;
            return;
        }

        public void calculatePercentagesPart()
        {
            percentagesPart = debtAfterCurrentPayment * (double)rate * 0.01 / 12.0;
            return;
        }

        public void calculateBodyPart()
        {
            bodyPart = AnnuityMonthlyPayment - percentagesPart;
            return;
        }
        //--------------------
    }
    public class printHeader
    {
        public printHeader()
        {
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Title = "Credit calculator";
            int height = 35;
            int WindowWidth = 200;
            Console.WindowHeight = height;
            int WindowTop = Console.WindowTop;
            WindowWidth = Console.WindowWidth;
            Console.Clear();
            Console.WriteLine("|Number |  Payment day\t  |  BodyPart\t| PercentagesPart|Remainder of loan|");
        }
    }
    public class preparing
    {
        public int rate = 10;
        public int creditSum = 100000;
        public int timePeriodOfCreditInMonth = 6;
        public DateTime startDate;

        public DateTime ReadDateFromConsole(String displayText)
        {
            Console.WriteLine(displayText);

            //   int year = IOHelpers.ReadIntFromConsole("\nEnter the year:", 1900, 2015);
            int year = 2015; // --------
            //   int month = IOHelpers.ReadIntFromConsole("Enter the month number:", 1, 12);
            int month = 1; // --------------
            //   int day = IOHelpers.ReadIntFromConsole("Enter the day:", 1, 31);
            int day = 5; // -------------

            return (new DateTime(year, month, day));
        }

        public preparing()
        {
            // Set the current culture to english
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            //        creditSum = CourseUtils.IOHelpers.ReadIntFromConsole("Input credit sum in $");
            //        rate = CourseUtils.IOHelpers.ReadIntFromConsole("Input rate in %");
            //       timePeriodOfCreditInMonth = CourseUtils.IOHelpers.ReadIntFromConsole("Input time period in months");
            startDate = ReadDateFromConsole("Please enter date of receiving of the credit");
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            preparing start = new preparing();
            printHeader heder = new printHeader();

            List<CurrentPayment> table = new List<CurrentPayment>();

            for (int i = 1; i <= 6; i++)
                table.Add(new CurrentPayment(i, start.creditSum, start.rate, start.timePeriodOfCreditInMonth, start.startDate));

            foreach (CurrentPayment i in table)
                Console.WriteLine(i);

            Console.ReadKey();
        }


    }
}
